using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class BaseContainer<T> : SingletonMB<T> where T : MonoBehaviour
{
	public List<Item> DisplayItems;

	public Button backBtn;

	public Button eshopBtn;

	public Transform content;

	public BuyButton buttonPrefab;

	private const ulong APP_UID = 72067558858375168uL;

	public Action OnCloseClicked;

	public ScrollRect scroll;

	public List<BuyButton> buyButtons = new List<BuyButton>();

	public int activeBtn;

	private bool delay;

	private float timer;

	private int NumOfActiveBtns => buyButtons.FindAll((BuyButton x) => x.isActiveAndEnabled).Count;

	public int ActiveBtn
	{
		get
		{
			return activeBtn;
		}
		set
		{
			if (delay)
			{
				delay = false;
				int num = value;
				if (value < 0)
				{
					num = 0;
				}
				else if (value > NumOfActiveBtns - 1)
				{
					num = NumOfActiveBtns - 1;
				}
				if (value < NumOfActiveBtns - 1)
				{
					MoveScroll(num > activeBtn);
				}
				activeBtn = num;
				ShowActiveIcon(num);
			}
		}
	}

	public void OnCloseClick()
	{
		OnCloseClicked?.Invoke();
	}

	private void MoveScroll(bool right)
	{
		if ((bool)scroll)
		{
			if (right)
			{
				scroll.horizontalNormalizedPosition += 0.1f;
			}
			else
			{
				scroll.horizontalNormalizedPosition -= 0.1f;
			}
		}
	}

	private void Start()
	{
		delay = true;
		timer = 0f;
		ActiveBtn = 0;
	}

	private void Update()
	{
		if (Singleton<InputController>.Instance.Action)
		{
			buyButtons[activeBtn].OnClick();
		}
		if (Singleton<InputController>.Instance.Horizontal == 0f)
		{
			delay = true;
			timer = 0f;
		}
		else if (Singleton<InputController>.Instance.Horizontal > 0f)
		{
			ActiveBtn++;
		}
		else if (Singleton<InputController>.Instance.Horizontal < 0f)
		{
			ActiveBtn--;
		}
		if (!delay)
		{
			timer += Time.deltaTime;
			if (timer > 0.3f)
			{
				delay = true;
				timer = 0f;
			}
		}
		if (Singleton<InputController>.Instance.Left)
		{
			ActiveBtn--;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Right)
		{
			ActiveBtn++;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Back)
		{
			backBtn.onClick.Invoke();
		}
		if (Singleton<InputController>.Instance.X && eshopBtn != null && eshopBtn.gameObject.activeSelf)
		{
			GoToEshop();
		}
	}

	public void GoToEshop()
	{
	}

	private void ShowActiveIcon(int activeBtnInd)
	{
		foreach (BuyButton buyButton in buyButtons)
		{
			buyButton.HideActiveIcon();
		}
		buyButtons[activeBtnInd].ShowActiveIcon();
	}

	public virtual void Fill()
	{
		foreach (Item displayItem in DisplayItems)
		{
			BuyButton buyButton = UnityEngine.Object.Instantiate(buttonPrefab, content);
			buyButton.Fill(displayItem);
			buyButtons.Add(buyButton);
			buyButton.OnClicked = (Action)Delegate.Combine(buyButton.OnClicked, new Action(OnClicked));
		}
		ActiveBtn = 0;
		Hide();
	}

	public void Hide()
	{
		base.gameObject.SetActive(value: false);
	}

	public void Show()
	{
		Refresh();
		SingletonMB<MessageCenter>.Instance.OnShopShow?.Invoke();
		base.gameObject.SetActive(value: true);
	}

	public virtual void Refresh()
	{
	}

	public virtual void OnClicked()
	{
	}
}
